package com.itheima.service.impl;

import com.itheima.common.utils.POIUtils;
import com.itheima.mapper.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderDao;

    /*
     * 根据日期修改可预约人数
     * */
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        orderDao.editNumberByOrderDate(orderSetting);
    }

    /*
     * 模板下载
     * */
    @Override
    public void download(String filename) {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx");
        try {
            //基于提供好的模板文件创建一个新的Excel表格对象
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = excel.getSheet("预约设置模板");
            sheet.getRow(0).getCell(0).setCellValue("日期");
            sheet.getRow(0).getCell(1).setCellValue("可预约数量");

            //通过输出流将文件下载到客户端浏览器中
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\MyJava_Project\\bxg-health-parent\\"+ filename);
            excel.write(fileOutputStream);

            //关闭资源
            fileOutputStream.flush();
            fileOutputStream.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * 根据年月查询对应的预约设置信息
     * */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String month) {
        //将字符串日期格式化为时间格式
        YearMonth yearMonth = YearMonth.parse(month, DateTimeFormatter.ofPattern("yyyy-M"));
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();

        return orderDao.getOrderSettingByMonth(firstDayOfMonth,lastDayOfMonth);
    }

    /*
     * 文件上传
     * */
    @Override
    public void upload(MultipartFile excelFile) {
        try {
            //读入excel文件，解析后返回
            List<String[]> list = POIUtils.readExcel(excelFile);
            //遍历list集合，将数据写入到数据库中
            for (int i = 1; i < list.size(); i++) {
                String orderDate = list.get(i)[0];
                String number = list.get(i)[1];
                //将单元格文本写入到数据库中
                orderDao.insert(orderDate,number);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
